/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.kth.lwr.jeat.samples.onsitetreatment.transportations;

import javax.measure.quantity.Duration;
import javax.measure.quantity.Length;
import javax.measure.quantity.Quantity;
import javax.measure.unit.NonSI;
import javax.measure.unit.SI;
import org.jscience.physics.amount.Amount;
import se.kth.lwr.jeat.core.*;
import se.kth.lwr.jeat.utils.IdleSupportedTransportation;
import se.kth.lwr.jeat.utils.ProductionSupport;

/**
 *
 * @author davidev
 */
public class SewageLorry extends IdleSupportedTransportation{
    
    
    
    ProductionSupport idleSupport;
    AmountDataSource repeat;
    DataSource<Amount> share;
    InventoryFunction currentMultiplier;

    
    

    public SewageLorry(AmountDataSource<Duration> repeat, DataSource<Amount> share) {
        super("Sewage Lorry");
        this.repeat=repeat;
        this.share=share;
        this.idleSupport=new ProductionSupport("Idle time", idleTransportEmissions, NonSI.MINUTE);
    }

    @Override
    public void setFill(double fill) {
        super.setFill(fill); 
        
    }
    
    private InventoryFunction getEmissions(double fill){
        return new InventoryFunction(fullTransportEmission).differenceTo(emptyTransportEmissions).multiply(getFillAsDataSource(fill)).merge(emptyTransportEmissions);        
    }
    
    private AmountDataSource getFillAsDataSource(double fill){
        return new AmountDataSource("fill", "Filling degree in sewage lorry", Amount.valueOf(fill*100,NonSI.PERCENT));
    }
    

    @Override
    protected Activity getIdleActivity(Amount<Duration> duration, String comment) {
        AmountFunction multiplyer=new AmountFunction();
        multiplyer.divide(repeat).divide(share);
        //idleSupport=new ProductionSupport("Idle time", idleTransportEmissions, NonSI.MINUTE);
        return idleSupport.getProduction(getName() +" in idle state", comment, duration,multiplyer);
    }

    @Override
    protected Activity getTransportActivity(Amount<Length> distance, double fill, String comment) {
        //create production support for current filling degree
        ProductionSupport support=new ProductionSupport("Transport distance", getEmissions(fill), SI.KILOMETER);
        //create and set up multiplier
        AmountFunction multiplier=new AmountFunction();
        multiplier.divide(repeat).divide(share);
        //create and return activity
        return support.getProduction(getName()+ " Transport",comment, distance,multiplier);
    }
    
    /**
     * Emissions for full transport (VEVA)
     *
     */
    
    public static final InventoryDataSource fullTransportEmission = new InventoryDataSource("fullTransportEmission",
            "Emissions for full transport (VEVA)",
            new Inventoriable() {
                @Override
                public Inventory getInventory() {
                    Inventory retval=Inventory.emptyInventory();
                    retval.addEnergyUse(EnergyCategory.FOSSIL, Amount.valueOf("20.3 MJ/km"));
                    return retval;                                     
                }});
    
    /**
     * Emissions for empty transport (VEVA)
     *
     */
    
    public static final InventoryDataSource emptyTransportEmissions = new InventoryDataSource("emptyTransportEmissions",
            "Emissions for empty transport (VEVA)",
            new Inventoriable() {
                @Override
                public Inventory getInventory() {
                    Inventory retval=Inventory.emptyInventory();
                    retval.addEnergyUse(EnergyCategory.FOSSIL, Amount.valueOf("15.2 MJ/km"));
                    return retval;  
                    
                    
                }});
    
    /**
     * Emissions during idle time
     *
     */
    
    public static final InventoryDataSource idleTransportEmissions = new InventoryDataSource("idleTransportEmissions",
            "Emissions during idle time",
            new Inventoriable() {
                @Override
                public Inventory getInventory() {
                    Inventory retval=Inventory.emptyInventory();
                    retval.addEnergyUse(EnergyCategory.FOSSIL, Amount.valueOf("2.4 MJ/min"));
                    return retval;  
                    
                    
                }});
    

    
}
